package xj.graph2d.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeListener;

public class TitledDialogPanel 
  extends OptionDialogPanel {

  public TitledDialogPanel(JFrame owner,
			   int style, 
			   ChangeListener listener,
			   DialogPanel[] options) {
    super(owner, style);
    this.listener = listener;
    this.options = options;

    setLayout(new BorderLayout());

    if (options != null && options.length > 0) {
      if (options.length == 1) {
	if (options[0] != null) {
	  add(options[0], BorderLayout.CENTER);
	  options[0].setListener(listener);
	}
      } else {
	tabbedPane = new JTabbedPane();
	add(tabbedPane, BorderLayout.CENTER);
	for (int i = 0; i < options.length; i++) {
	  if (options[0] != null) {
	    tabbedPane.addTab(options[i].getName(), options[i]);
	    options[i].setListener(listener);
	  }
	}
      }
    }
  }

  public void setOptions() {
    if (options != null) {
      for (int i = 0; i < options.length; i++) {
	if (options[i] != null) {
	  options[i].setOption();
	}
      }
    }
  }

  public void setTitlePaneText(String header, String[] text) {
    setTitlePaneText(header, text, null);
  }

  public void setTitlePaneText(String header, 
			       String[] text,
			       Icon icon) {
    if (titlePane == null) { 
      titlePane = new TitlePane();
      add(titlePane, BorderLayout.NORTH);
    }
    titlePane.setText(header, text);
    if (icon != null) { 
      titlePane.setIcon(icon);
    }
  }

  @Override
  public void applyOptions() { 
    if (options.length == 1) {
      options[0].applyOption();
    } else { 
      if (tabbedPane != null) { 
	Component comp = tabbedPane.getSelectedComponent();
	if (comp instanceof DialogPanel) { 
	  DialogPanel panel = (DialogPanel) comp;
	  panel.applyOption();
	}
      }
    }
  }
  
  protected TitlePane titlePane = null;
  
  protected ChangeListener listener;

  protected DialogPanel[] options;
  
  protected JTabbedPane tabbedPane = null;

}

class TitlePane extends JPanel {
  
  public TitlePane() {
    this(null, null);
  }
  
  public TitlePane(String header, String[] lines) {
    this.header = header;
    this.lines = lines;
    setPreferredSize(new Dimension(200, 68));
    setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
  }

  @Override
  public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
			RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    Dimension dim = getSize();
    Insets insets = getInsets(); 
    GradientPaint gp = new GradientPaint(0, 0, Color.white, dim.width, 0,
					 Color.lightGray);
    g2.setPaint(gp);
    g2.fillRect(0, 0, dim.width, dim.height);

    Font f = g2.getFont();
    g2.setColor(Color.black);
    if (header != null) {
      Font f1 = f.deriveFont(Font.BOLD, 16);
      g2.setFont(f1);
      g2.drawString(header, 10, 20);
    }
    if (lines != null) {
      Font f2 = f.deriveFont(Font.PLAIN, 12);
      g2.setFont(f2);
      if (lines[0] != null) {
	g2.drawString(lines[0], 30, 40);
      }
      if (lines[1] != null) {
	g2.drawString(lines[1], 30, 55);
      }
    }

    if (icon != null) { 
      int iwidth = icon.getIconHeight();
      int iheight = icon.getIconWidth();
      icon.paintIcon(this, g, dim.width - iwidth - insets.right - 2, insets.top); 
    }
  }

  public String getHeader() {
    return header;
  }

  public void setHeader(String header) {
    this.header = header;
  }

  public String[] getLines() {
    return lines;
  }

  public void setLines(String[] lines) {
    this.lines = lines;
  }
  
  public void setText(String header, String[] lines) {
    this.header = header;
    this.lines = lines;
  }

  public Icon getIcon() { 
    return icon;
  }

  public void setIcon(Icon icon) { 
    this.icon = icon; 
  }

  String header;
  String[] lines;
  Icon icon;

}
